package com.yswl.yeeduo;

import android.app.Application;
import android.content.Context;
import cn.jpush.android.api.JPushInterface;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nrs.utils.tools.CrashHandler;
import com.yswl.yeeduo.tool.KLog;
import com.yswl.yeeduo.tool.KStringTool;
import com.yswl.yeeduo.ui.tool.PositionUtil;
import com.yswl.yeeduo.ui.tool.ServiceAuthManager;

/**
 * 87:F6:4C:08:1C:25:FC:F8:22:DA:DB:DB:FF:FE:EB:99:D4:6F:A3:F2
 * 
 * @author Administrator
 * 
 */
public class App extends Application {
	/** App */
	private static App share;

	/** 获取实例 */
	public static App getInstance() {
		return share;
	}

	/**
	 * 用户位置
	 */
	public String latitude ;
	public String longitude ;

	public boolean isLogin = false;

	private boolean getUserLoginState() {
		String userId = ServiceAuthManager.getUserId(this);
		return KStringTool.isNotEmpty(userId);
	}

	@Override
	public void onCreate() {
		super.onCreate();

		share = this;
		setDebug(false);
		isLogin = getUserLoginState();
		intiJpush();
		initImageLoader(share);// 第三方图片异步加载

		initPosition();
		//初始化  错误日志输出在本地
		CrashHandler.getInstance().init(this);
	}

	private void initPosition() {
		latitude = PositionUtil.getSavePosition(getApplicationContext())[0];
		longitude = PositionUtil.getSavePosition(getApplicationContext())[1];
	}

	private void intiJpush() {
		JPushInterface.setDebugMode(KLog.isShow);
		JPushInterface.init(this);
	}

	// 设置 debug 模式
	public void setDebug(boolean isDebug) {
		KLog.isShow = isDebug;
	}

	// 第三方图片
	public void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you
		// may tune some of them,
		// or you can create default configuration by
		// ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
		.threadPriority(Thread.NORM_PRIORITY - 2)
		.denyCacheImageMultipleSizesInMemory()
		.discCacheFileNameGenerator(new Md5FileNameGenerator())
		.tasksProcessingOrder(QueueProcessingType.LIFO)
		.writeDebugLogs() // Remove
		.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}

}
